<template>
  <!-- 参与项目列表-->
  <div class="app-container">
    <!-- 搜索表单 -->
    <div class="form-container">
      <el-form
        ref="queryForm"
        :model="filters"
        :inline="true"
        label-width="auto"
      >
        <el-form-item label="项目编号" prop="proNum">
          <el-input v-model="filters.proNum" placeholder="请输入项目编号" clearable size="small" />
        </el-form-item>
        <el-form-item label="项目名称" prop="proName">
          <el-input v-model="filters.proName" placeholder="请输入项目名称" clearable size="small" />
        </el-form-item>
        <el-form-item label="项目类型" prop="proTypeId">
          <el-select v-model="filters.proTypeId" placeholder="请选择项目类型" clearable size="small">
            <el-option
              v-for="dict in proTypeList"
              :key="dict.id"
              :label="dict.typeName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <el-select v-model="filters.status" clearable size="small">
            <el-option
              v-for="dict in ProStatusList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <!-- 高级设置一栏 start -->
        <el-form-item>
          <slot name="label">
            <i class="highLevelStyle" :class="highLevelSet===true?'el-icon-arrow-up':'el-icon-arrow-down'" @click="highLevelSet = !highLevelSet" />
          </slot>
        </el-form-item>
        <el-row v-if="highLevelSet">
          <el-form-item label="项目地址" prop="status">
            <el-cascader
              v-model="filters.currentProvince"
              size="small"
              placeholder="请选择所在区域"
              :props="cityNormalizer"
              :options="cityOptions"
              clearable
              style="width: 100%"
              @change="changeCitySelect"
            >
              <template slot-scope="{ data }">
                <span>{{ data.label }}</span>
              </template>
            </el-cascader>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="true"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!-- 操作按钮 -->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" :disabled="single" size="middle" @click="handleDetail">详情</el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 列表 -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :row-style="rowClass"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="proName"
        label="项目名称"
        min-width="180"
        align="left"
        header-align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="proNum"
        label="项目编号"
        min-width="120"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="grade"
        label="项目等级"
        min-width="80"
        align="center"
        :show-overflow-tooltip="true"
        :formatter="formatGrade"
      />
      <el-table-column
        prop="proTypeName"
        label="项目类型"
        min-width="80"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="项目状态"
        min-width="80 "
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status === ''">-</span>
          <span v-else>{{ scope.row.status | ProStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="计划开始时间"
        align="center"
        width="110"
        prop="startTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="计划结束时间"
        align="center"
        width="110 "
        prop="endTime"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
/** 页面接口 start */
// 项目类型
import { getProTypeListNo } from '@/api/superviseBaseInfo/typeManage/projectType'
import { getTeamProjectList } from '@/api/supplier/JoinProject'
import { listByTreeProcessType } from '@/api/common/area.js'
/** 页面接口 end */
import { mapState } from 'vuex'
export default {
  name: 'JoinProject',
  data() {
    return {
      // 项目类型
      proTypeList: [],
      cityOptions: [], // 城市数据
      cityNormalizer: {
        value: 'value',
        label: 'label',
        children: 'children'
      },
      /* 搜索条件 start*/
      highLevelSet: false, // 高级搜索
      dateRange: [], // 时间范围数组
      filters: { // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        proNum: '', // 项目编号
        proName: '', // 项目名称
        region: '',
        province: '',
        city: '',
        county: '',
        enterpId: '', //  对方企业id:
        status: '1', // 项目状态
        proTypeId: '' // 项目类型
      },
      /* 搜索条件 end*/

      // 选择框标题
      multiple: true, // 非多个禁用
      /* 列表 start*/
      tableData: [], // 列表
      selectRow: [], // 高亮数组
      loading: false,
      multipleSelection: [], // 选中的行对象
      total: 0 // 总条数
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView,
      ProStatusList: (state) => state.basicInfo.ProStatusList,
      proGrade: state => state.basicInfo.proGrade
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    },
    single() {
      return this.multipleSelection.length === 0
    }
  },
  watch: {
    $route(to, from) {
      this.$router.go(0)
    }
  },
  mounted() {
    const { id } = this.$route.query
    this.filters.enterpId = id
    this.getList()
    this.getProjectType()
    this.getRegionTree()
  },
  methods: {
    formatGrade(row) {
      return this.selectDictLabel(this.proGrade, row.grade)
    },
    // 选择地区拆分编码
    changeCitySelect(values) {
      const [aaaa, region, province, city, county] = values
      console.log(aaaa)
      this.filters = { ...this.filters, region, province, city, county }
    },
    // 获取区域树
    async getRegionTree() {
      const { data: { rows }} = await listByTreeProcessType({ regionCode: '' })
      this.cityOptions = this.deleteChildren(rows)
    },
    // 获取项目类型
    async getProjectType() {
      const { data: { rows }} = await getProTypeListNo()
      this.proTypeList = rows
    },
    /* 列表 start*/
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 多选框选中数据
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 获取列表
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await getTeamProjectList(this.filters)
        this.tableData = rows
        this.total = total
        this.loading = false
        // this.addTaleData()
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      const arr = {}
      if (this.tableData.length < this.filters.pageSize) {
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 搜索方法
    handleQuery() {
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.orgLeaderName = ''
      this.dateRange = []
      this.handleQuery()
    },
    // 详情
    handleDetail() {
      this.$router.push({
        name: 'JoinProjectDetail',
        params: { row: this.multipleSelection[0] }
      })
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
